package com.douwong.student.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.douwong.student.R;
import com.douwong.student.contant.Config;
import com.douwong.student.utils.UIUtils;


/**
 * @autor yichao
 * @time 2017/04/1011:21
 * @des 应用和在线学习的基类
 */
public abstract class BaseAppFragment extends BaseFragment {
    protected GridView mAppGv;
    protected RelativeLayout mRlFreshContainer;
    protected LinearLayout mLlLoading;
    @Override
    protected void initData() {
        super.initData();
        getActivity().registerReceiver(mReceiver,mIntentFilter);
        loadData();
        mAppGv.setSelector(new ColorDrawable(Color.TRANSPARENT));
    }

    @Override
    protected View getRootView(LayoutInflater inflater, ViewGroup container) {
        View viewRoot= UIUtils.inflate(getActivity(),R.layout.fgm_application);
        mAppGv = (GridView) viewRoot.findViewById(R.id.appliction_gv);
        mRlFreshContainer = (RelativeLayout) viewRoot.findViewById(R.id.application_refreshcontainer);//刷新根布局
        mLlLoading = (LinearLayout) viewRoot.findViewById(R.id.application_refresh_pregresslayout);//加载中
        return viewRoot;

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mRlFreshContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击加载
                loadData();
            }
        });
        mAppGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BaseAppFragment.this.onItemClick(position);
            }
        });

    }
    IntentFilter mIntentFilter=new IntentFilter(){

        {
            addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            addAction(Config.TURN_ON_PATRIARCH);
            addAction(Config.TURN_OFF_PATRIARCH);
        }
    };

    BroadcastReceiver mReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
                //获取联网状态的NetworkInfo对象
                NetworkInfo info = intent
                        .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                if (info != null) {
                    //如果当前的网络连接成功并且网络连接可用
                    if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
                        if (info.getType() == ConnectivityManager.TYPE_WIFI
                                || info.getType() == ConnectivityManager.TYPE_MOBILE) {
                            //网络可用
                            loadData();

                        }
                    }
                }
            }else if (Config.TURN_OFF_PATRIARCH.equals(intent.getAction())){

                //关闭家长模式
                turnOffPatriarch();
            }else if (Config.TURN_ON_PATRIARCH.equals(intent.getAction())){
                //打开家长模式
                turnOnPatriarch();


            }

        }
    };



    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mReceiver!=null){
            getActivity().unregisterReceiver(mReceiver);
            mReceiver=null;
        }

    }
    /**
     * 打开家长模式
     */
    protected  void turnOnPatriarch(){};
    /**
     * 关闭家长模式
     * */
    protected  void turnOffPatriarch(){};

    /**
     * 加载数据
     */
    protected abstract void loadData();

    /**
     * 点击某个位置
     * @param position
     */
    protected abstract void onItemClick(int position);
}
